class Solution {
public:
    vector<int> executeInstructions(int n, vector<int>& startPos, string s) {
        vector<int> res(s.size());
        int x, y;
        x = startPos[1], y = startPos[0];
        for (int i = 0; i < s.size(); i ++ ) {
            int dx = x, dy = y, cnt = 0;
            for (int j = i; j < s.size(); j ++ ) {
                if (s[j] == 'D') dy ++ ;
                else if (s[j] == 'U') dy -- ;
                else if (s[j] == 'L') dx -- ;
                else dx ++ ;
                if (dx >= 0 && dy >= 0 && dx < n && dy < n) cnt ++;
                else break;
            }
            res[i] = cnt;
        }
        return res;
    }
};
